package com.server.systems;

import java.io.Serializable;

import com.server.projectiles.projectileImpl;
import com.server.runtime.simulationImpl;

import combat.protocol.data.DoublePoint;
import combat.protocol.interfaces.OnboardSystemInt;
import combat.protocol.interfaces.TeamInt;
import combat.protocol.interfaces.VehicleInt;
import combat.protocol.types.ProjectileType;
import combat.protocol.interfaces.OnboardWeaponSystemInt; 

import combat.protocol.types.OnboardSystemStatus;
import combat.protocol.types.OnboardSystemType;
import combat.protocol.types.WeaponSystemType;

public class weaponSystemImpl extends onBoardSystemImpl implements OnboardWeaponSystemInt, Serializable {

	private static final long serialVersionUID = 1L;
	private int ammo = 0;
	private WeaponSystemType systemType = WeaponSystemType.UNKNOWN;
	
	public weaponSystemImpl() {
		super(OnboardSystemStatus.ONLINE, OnboardSystemType.WEAPON);
	}

	public weaponSystemImpl(int ammo, WeaponSystemType weaponSystemType) {
		super(OnboardSystemStatus.ONLINE, OnboardSystemType.WEAPON);
		this.ammo = ammo;
		this.systemType = weaponSystemType;
	}

	public int getAmmo() {
		return this.ammo;
	}

	public void setAmmo(int ammo) {
		this.ammo = ammo;
	}

	public void fire(Object target) {
		if (getAmmo() > 0 && getOnboardSystemStatus().equals(OnboardSystemStatus.ONLINE)) {
			DoublePoint weaponPosition = null;
			TeamInt weaponTeam = null;
			// Get sensor(vehicle) position
			for (TeamInt team : simulationImpl.getReference().getTeams())
				for (VehicleInt vehicle : team.getVehicles())
					for (OnboardSystemInt onboardSystem : vehicle.getOnboardSystems())
						if (this.equals(onboardSystem)) {
							weaponTeam = team;
							weaponPosition = vehicle.getPosition();
						}
			if (weaponTeam != null && weaponPosition != null) {
				projectileImpl newProjectile = null;
				switch (getWeaponSystemType()) {
					case UNKNOWN:
						newProjectile = new projectileImpl(weaponPosition, target, 10.0, ProjectileType.UNKNOWN);
						break;
					case UNGUIDED_MISSILELAUNCHER:
						newProjectile = new projectileImpl(weaponPosition, target, 20.0, ProjectileType.MISSILE_UNGUIDED);
						break;
					case GUIDED_MISSILELAUNCHER:
						newProjectile = new projectileImpl(weaponPosition, target,  20.0, ProjectileType.MISSILE_GUIDED);
						break;
					case TORPEDOLAUNCHER:
						newProjectile = new projectileImpl(weaponPosition, target, 25.0, ProjectileType.TORPEDO);
						break;
					case GOALKEEPER:
						// TODO action of goalkepper
						break;
					default:
						newProjectile = new projectileImpl(weaponPosition, target, 25.0, ProjectileType.UNKNOWN);
						break;
				}
				if (newProjectile != null)
					weaponTeam.addProjectile(newProjectile);
			}
		}
	}

	public WeaponSystemType getWeaponSystemType() {
		return this.systemType;
	}

	public void setWeaponSystemType(WeaponSystemType weaponSystemType) {
		this.systemType = weaponSystemType;
	}
}
